package com.yyl.fmall.shopcart.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yyl.fmall.shopcart.entity.ShoppingCart;
import com.yyl.fmall.shopcart.service.IShoppingCartService;
import com.yyl.fmall.utils.CommonConstants;
import com.yyl.fmall.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 购物车  前端控制器
 * </p>
 *
 * @author YuanSir
 * @since 2022-11-08
 */
@RestController
@RequestMapping("/shopcart")
@CrossOrigin
public class ShoppingCartController {
    
    @Autowired
    private IShoppingCartService shoppingCartService;


    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
        
        shoppingCart.setCartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    
        shoppingCartService.save(shoppingCart);
        
        return R.ok();
    
    }
    
    
    
    @GetMapping("/list")
    public R list(Integer userId){
    
        List<ShoppingCart> shoppingCartList = shoppingCartService.getList(userId);
        
        return R.ok().put(CommonConstants.R_MAP_KEY, shoppingCartList);
    
    }
    
    
    @PutMapping("/update/{cartId}/{cartNum}")
    public R update(@PathVariable("cartId") Integer cartId, @PathVariable("cartNum") Integer cartNum){
    
        shoppingCartService.update(new LambdaUpdateWrapper<ShoppingCart>()
                .set(ShoppingCart::getCartNum, cartNum)
                .eq(ShoppingCart::getCartId, cartId));
        
        return R.ok();
        
    }
    
    @GetMapping("/listbycids")
    public R listByCids(String cids){
    
        List<ShoppingCart> shoppingCartList = shoppingCartService.listByCids(cids);
        
        return R.ok().put(CommonConstants.R_MAP_KEY, shoppingCartList);
    
    }
    
    

}
